/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-08-27 16:05:42
 * @filename : sea-modules/imglazyload/zepto.imglazyload.js
 * @version : 
 * @description : 
 */


(function(){
   var imgReady = (function () {
       var list = [], intervalId = null,

	    tick = function () {
		var i = 0;
		for (; i < list.length; i++) {
		    list[i].end ? list.splice(i--, 1) : list[i]();
		};
		!list.length && stop();
	    },

	    stop = function () {
		clearInterval(intervalId);
		intervalId = null;
	    };

       return function (url, ready, load, error) {
	    var onready, width, height, newWidth, newHeight,
	    img = new Image(); 
	    img.src = url;

	    if (img.complete) {
		ready.call(img);
		load && load.call(img);
		return;
	    };
	    
	    width = img.width;
	    height = img.height;
	    
	    img.onerror = function () {
		error && error.call(img);
		onready.end = true;
		img = img.onload = img.onerror = null;
	    };
	    
	    onready = function () {
		newWidth = img.width;
		newHeight = img.height;
		if (newWidth !== width || newHeight !== height ||
		
		    newWidth * newHeight > 1024
		) {
		    ready.call(img);
		    onready.end = true;
		};
	    };
	    onready();
	    

	    img.onload = function () {
		
		!onready.end && onready();
	    
		load && load.call(img);
		
		
		img = img.onload = img.onerror = null;
	    };

	    if (!onready.end) {
		list.push(onready);
	    
		if (intervalId === null) intervalId = setInterval(tick, 40);
	    };
	};
    })();

  
    $.fn.imglazyload = function(opt) {

	var opt = opt || {}, tthis = this;
	 
 
	var defaults = {
  	          imgClass : 'imglazyload',
              sW : null,
              sH : null,
              transform : true, 
              screen : true, 
              offset : 0,
              startY : 0,  
              endY: $(window).height()
	    };
	 
        for (var i in defaults ) {
           if (typeof(opt[i]) == 'undefined') {
              opt[i] = defaults[i];
           }
        }

        $(tthis).find('.'+opt.imgClass).each(function(){
            var that = this,
                topPX = $(this).offset().top - opt.offset;

            opt.sW = (opt.sW) ? opt.sW : opt.sW = $(this).parent().width();
            opt.sH = (opt.sH) ? opt.sH : opt.sH = $(this).parent().height();
      
            if (topPX >= opt.startY && topPX < opt.endY && opt.sW > 0 && opt.sH > 0) {
                 var url = $(this).attr('url');
                 imgReady(url,function(){},function(){
                       var imgW = this.width,
                           imgH = this.height;
                       if (opt.screen) {
                          if (imgW/opt.sW > imgH/opt.sH) {
                              if (opt.transform) {
                                 var px =  -((imgW*opt.sH/imgH)-opt.sW)/2;
                                 $(that).attr({
                                     'src' : $(that).attr('url'),
                                     'style' : "height:"+opt.sH+"px;-moz-transform:translate("+px+"px);-webkit-transform:translate("+px+"px)"
                                 }).removeClass("imglazyload");
                              } else {
                                 $(that).attr({
                                     'src' : $(that).attr('url'),
                                     'style' : 'height:'+opt.sH+'px'
                                 }).removeClass("imglazyload");
                              }
                          } else {
                                 $(that).attr({
                                     'src' : $(that).attr('url'),
                                     'style' : 'width:'+opt.sW+'px'
                                 }).removeClass("imglazyload");
                          } 
                       } else {
                                 $(that).attr('src',$(that).attr('url')).removeClass("imglazyload")
                       }
                  })
            }
         });
     }; 
    
     if (typeof define === "function" ) {
        define("imglazyload/zepto.imglazyload.js", [], function () { return $.fn.imgloazyload;} );
     }
 
})();


